﻿using BLL;
using Model;
using System;
using System.Text;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditBlock : FormPage
    {
        private Lc_BlockBLL lbb = new Lc_BlockBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                decimal moduleKey = Convert.ToDecimal(FORM_KEY);
                var entity = lbb.GetEntity(moduleKey);
                ModuleName.Text = entity.C_BLOCK_NAME;
                ModulePath.Text = entity.C_BLOCK_DEPATH;
                ModuleRemark.Text = entity.REMARK;
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_IFEDIT)
            {
                decimal moduleKey = Convert.ToDecimal(FORM_KEY);
                var moduleName = ModuleName.Text.Trim();
                var modulePath = ModulePath.Text.Trim();
                var moduleRemark = ModuleRemark.Text;
                if (moduleName == "")
                {
                    MessageBox.Show(this, "【温馨提醒】请输入模块名称");
                    return;
                }
                if (modulePath == "")
                {
                    MessageBox.Show(this, "【温馨提醒】请输入模块路径");
                    return;
                }
                var entity = lbb.GetEntity(moduleKey);
                entity.C_BLOCK_NAME = CurrentUserName;
                entity.C_BLOCK_NAME = moduleName;
                entity.C_BLOCK_DEPATH = modulePath;
                entity.REMARK = moduleRemark;
                var c = lbb.UPDATE(entity, CurrentUserName);
                if (c > 0)
                {
                    MessageBox.Show(this, "【温馨提醒】修改成功");
                }
                else
                {
                    MessageBox.Show(this, "【温馨提醒】修改失败！");
                }
            }
            else
            {
                var moduleName = ModuleName.Text.Trim();
                var modulePath = ModulePath.Text.Trim();
                var moduleRemark = ModuleRemark.Text;
                var entity = new Lc_Block
                {
                    C_BLOCK_NAME = moduleName,
                    C_BLOCK_DEPATH = modulePath,
                    REMARK = moduleRemark
                };
                if (lbb.INSERT(entity, CurrentUserName) > 0)
                {
                    FORM_IFEDIT = true;
                    FORM_KEY = entity.N_BLOCK_ID.ToString();

                    var Builder = new StringBuilder();
                    Builder.AppendLine("   var option={");
                    Builder.AppendLine("                Msg:\"【温馨提示】添加成功！\",");
                    Builder.AppendLine("                YesCallBack:function(index){");
                    Builder.AppendLine("                                FORM_UPDATE_TITLE(\"编辑模块\");");
                    Builder.AppendLine("                                top.layer.close(index);");
                    Builder.AppendLine("                }");
                    Builder.AppendLine("   };");
                    Builder.AppendLine("   top.MessageBox.Alert(option);");
                    MessageBox.ResponseScript(this, Builder.ToString());
                }
                else
                {
                    MessageBox.Show(this, "【温馨提醒】新增失败！");
                }
            }
        }
    }
}